package ginhelper

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// swagger:model
type RestResult struct {
	Code    int         `json:"code"`     // 代码
	Msg     string      `json:"errorMsg"` // 消息
	Data    interface{} `json:"data"`     // 数据集
	Success bool        `json:"success"`  // 是否成功
}

func NewRestResult() *RestResult {
	result := new(RestResult)
	result.Success = true
	result.Code = http.StatusOK
	return result
}

func (r *RestResult) BuildSuccessResultMsg(msg string) *RestResult {
	r.Msg = msg
	r.Success = true
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) BuildSuccessResultData(data interface{}) *RestResult {
	r.Data = data
	r.Success = true
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) BuildSuccessResultDataMsg(data interface{}, msg string) *RestResult {
	r.Data = data
	r.Msg = msg
	r.Success = true
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) BuildFailedResultMsg(msg string) *RestResult {
	r.Msg = msg
	r.Success = false
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) BuildFailedResultData(data interface{}) *RestResult {
	r.Data = data
	r.Success = false
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) BuildFailedResultDataMsg(data interface{}, msg string) *RestResult {
	r.Data = data
	r.Msg = msg
	r.Success = false
	r.Code = http.StatusOK
	return r
}

func (r *RestResult) SuccessWithMsg(c *gin.Context, msg string) {
	restResult(c, http.StatusOK, msg, "", true)
}

func (r *RestResult) SuccessWithData(c *gin.Context, data interface{}) {
	restResult(c, http.StatusOK, "", data, true)
}

func (r *RestResult) SuccessWithDataMsg(c *gin.Context, data interface{}, msg string) {
	restResult(c, http.StatusOK, msg, data, true)
}

func (r *RestResult) FailedWithMsg(c *gin.Context, msg string) {
	restResult(c, http.StatusBadRequest, msg, "", false)
}

func (r *RestResult) FailedWithData(c *gin.Context, data interface{}) {
	restResult(c, http.StatusBadRequest, "", data, false)
}

func (r *RestResult) FailedWithDataMsg(c *gin.Context, data interface{}, msg string) {
	restResult(c, http.StatusBadRequest, msg, data, false)
}

func restResult(c *gin.Context, httpCode int, msg string, data interface{}, isSuccess bool) {
	result := NewRestResult()
	result.Msg = msg
	result.Data = data
	result.Success = isSuccess

	c.JSON(httpCode, result)
}

func PrintResult(c *gin.Context, result RestResult) {
	restResult(c, result.Code, result.Msg, result.Data, result.Success)
}

func Success(c *gin.Context) {
	restResult(c, http.StatusOK, "", nil, true)
}

func SuccessWithMsg(c *gin.Context, msg string) {
	restResult(c, http.StatusOK, msg, nil, true)
}

func SuccessWithData(c *gin.Context, data interface{}) {
	restResult(c, http.StatusOK, "", data, true)
}

func SuccessWithDataMsg(c *gin.Context, data interface{}, msg string) {
	restResult(c, http.StatusOK, msg, data, true)
}

func Failed(c *gin.Context) {
	restResult(c, http.StatusBadRequest, "", nil, false)
}

func FailedWithMsg(c *gin.Context, msg string) {
	restResult(c, http.StatusBadRequest, msg, nil, false)
}

func FailedWithData(c *gin.Context, data interface{}) {
	restResult(c, http.StatusBadRequest, "", data, false)
}

func FailedWithDataMsg(c *gin.Context, data interface{}, msg string) {
	restResult(c, http.StatusBadRequest, msg, data, false)
}
